package com.wealth.vouchermarket.web.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wealth.commodity.entity.Goods;
import com.wealth.commodity.service.IGoodsService;
import com.wealth.member.entity.Address;
import com.wealth.member.entity.Member;
import com.wealth.member.service.IAddressService;
import com.wealth.member.service.IMemberService;
import com.wealth.merchant.enums.MarketEnum;
import com.wealth.order.service.IOrderPlatService;
import com.wealth.system.web.controller.BaseController;

import iaas.annotation.Login;
import iaas.utils.ResultObject;

/**
 * 惠生活订单
 * 
 * @author chan
 */
@Controller
@RequestMapping(value="/vouchermarket/order")
public class VoucherMartketOrderController extends BaseController{

	Logger logger = Logger.getLogger(VoucherMartketOrderController.class);
	@Autowired
	private IOrderPlatService orderPlatService;
	@Autowired
	private IGoodsService goodsService;
	@Autowired
	private IAddressService addressService;
	@Autowired
	private IMemberService memberService;
	
	/** 
	 * 创建订单页面 
	 */
	@RequestMapping(value="/create")
	@Login
	public String createForm(Model model,@RequestParam Long goodsid) {
		try{
			model.addAttribute("marketid",MarketEnum.LIFE.getValue());

			Goods goods = goodsService.get(goodsid);
			model.addAttribute("goods",goods);
			
			Address address = addressService.getAddressDefault(memberid);
			model.addAttribute("address",address);
			
			Member member = memberService.get(memberid);
			model.addAttribute("member",member);
			
					
			return "vouchermarket/order/create";
		}catch(Exception e){
			logger.error("惠生活订单创建页面错误", e);
			return "system/500";
		}
	}

	/** 
	 * 创建订单
	 */
	@RequestMapping(value="/create",method=RequestMethod.POST)
	@ResponseBody
	public ResultObject create(
			@RequestParam Long goodsid,
			@RequestParam Integer amount,
			@RequestParam Integer paymode ) {
		try{
			return orderPlatService.saveOrderForHuiLife(corpid, memberid, goodsid, amount, null, paymode);
		}catch(Exception e){
			return new ResultObject("999", "创建订单错误", "");
		}
	}
	
}
